# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=headers
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
pkgdesc="MinGW-w64 headers for Windows (mingw-w64)"
pkgver=12.0.0.r747.g1a99f8514
pkgrel=1
_commit='1a99f85148242b15b5f1fec1d4a318c1869a5816'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.mingw-w64.org/"
msys2_repository_url="https://sourceforge.net/p/mingw-w64/mingw-w64/ci/master/tree/mingw-w64-headers/"
license=('spdx:ZPL-2.1 AND LGPL-2.1-or-later')
groups=("${MINGW_PACKAGE_PREFIX}-toolchain")
makedepends=("${MINGW_PACKAGE_PREFIX}-tools"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             'git')
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
options=('!strip' '!emptydirs')
source=("mingw-w64"::"git+https://git.code.sf.net/p/mingw-w64/mingw-w64#commit=$_commit"
        0001-remove-ireference-boolean.patch)
sha256sums=('b1f7f5b22a61a03cafc9d9f6b211451bd9651e3395b1897d2fd52b93b5f1d26c'
            'cad48616325b5e70afa4e02424021fc8d54e17dcbf86eae3df990d3500080236')

pkgver() {
  cd "${srcdir}/mingw-w64"
  git describe --long ${_commit} | sed 's/\([^-]*-g\)/r\1/;s/-/./g;s/^v//g'
}

prepare() {
  cd "${srcdir}/mingw-w64"
  git apply "${srcdir}/0001-remove-ireference-boolean.patch"
}

build() {
  declare -a _extra_config
  if [[ ${MSYSTEM} == MINGW* ]]; then # MINGW32 & MINGW64
      _extra_config+=("--with-default-win32-winnt=0x601") # Windows 7
  elif [[ ${MSYSTEM} != CLANGARM64 ]]; then # UCR64 & CLANG64
      _extra_config+=("--with-default-win32-winnt=0x603") # Windows 8.1
  else # CLANGARM64
      _extra_config+=("--with-default-win32-winnt=0xA00") # Windows 10
  fi

  if [[ $MINGW_PACKAGE_PREFIX == *-clang-* ]] || [[ $MINGW_PACKAGE_PREFIX == *-ucrt-* ]]; then
    _extra_config+=("--with-default-msvcrt=ucrt")
  else
    _extra_config+=("--with-default-msvcrt=msvcrt")
  fi

  mkdir -p "${srcdir}"/headers-${MSYSTEM} && cd "${srcdir}"/headers-${MSYSTEM}

  "${srcdir}"/mingw-w64/mingw-w64-headers/configure \
    --prefix=${MINGW_PREFIX} \
    --enable-sdk=all \
    "${_extra_config[@]}" \
    --enable-idl \
    --without-widl
}

package() {
  cd "${srcdir}"/headers-${MSYSTEM}
  make DESTDIR="${pkgdir}" install

  rm "${pkgdir}"${MINGW_PREFIX}/include/pthread_signal.h
  rm "${pkgdir}"${MINGW_PREFIX}/include/pthread_time.h
  rm "${pkgdir}"${MINGW_PREFIX}/include/pthread_unistd.h

  install -Dm644 "${srcdir}"/mingw-w64/mingw-w64-headers/ddk/readme.txt "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/ddk-readme.txt
  install -Dm644 "${srcdir}"/mingw-w64/COPYING "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
  install -Dm644 "${srcdir}"/mingw-w64/COPYING.MinGW-w64/COPYING.MinGW-w64.txt "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.MinGW-w64.txt
  install -Dm644 "${srcdir}"/mingw-w64/COPYING.MinGW-w64-runtime/COPYING.MinGW-w64-runtime.txt "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.MinGW-w64-runtime.txt
}
